﻿using Microsoft.AspNetCore.Mvc;

namespace BlazorWebAssemblyUpload.Server.Controllers;
[ApiController]
[Route("[controller]")]
public class UploadController:ControllerBase
{
    public UploadController(IHostEnvironment env)
    {
        Env = env;
    }

    public IHostEnvironment Env { get; }

    [HttpPost]
    public async Task<IResult> Post([FromForm] IEnumerable<IFormFile> files)
    {
        var path = $"{Env.ContentRootPath}/upload/";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        foreach (var file in files)
        {

            var fileName = $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}{Path.GetExtension(file.FileName)}";

            await using FileStream fs = new(path + fileName, FileMode.Create);
            await file.OpenReadStream().CopyToAsync(fs);
        }
        return Results.Ok();
    }
}
